home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MPW_TOOL / TOOLS / TOOLS_WI / ICON_8 / MEMMON_F / MOUTPUT.C < prev    next >
Text File  |  1990-03-02  |  7KB  |  254 lines

  1. /*
  2.  * moutput.c: device-independent output routines.
  3.  */
  4.  
  5. #include "memmon.h"
  6.  
  7. /*
  8.  *  Prototypes.
  9.  */
  10.  
  11. hidden novalue setdisp Params((struct region *r, int key, word addr));
  12. hidden novalue mpaint Params((word addr, word len, int c1, int c2));
  13.  
  14. static int oldscale = 0;        /* previous output scaling */
  15. static int npauses = 0;
  16. static int reply = 0;
  17.  
  18.  
  19. /*
  20.  * refresh() - redraw screen, initially or after garbage collection.
  21.  */
  22. novalue refresh()
  23.    {
  24.    word nbytes, newpixels;
  25.    int newrows, newscale;
  26.    char sbuf[50];
  27.  
  28.    /* set display parameters for each region */
  29.    setdisp(&stc, 'f', (word)0);
  30.    setdisp(&str, 's', stc.saddr + stc.displ);
  31.    setdisp(&blk, 'b', str.saddr + str.displ);
  32.    if (textrow > 0)
  33.       memheight = height - (TextLines * textrow) - textsep + 1;
  34.    else
  35.       memheight = height;
  36.  
  37.    /* calc total number of bytes */
  38.    nbytes = stc.displ + str.displ + blk.displ;
  39.    if (stc.displ + str.displ + blk.displ == 0) {
  40.       fprintf(stderr, "%s: no regions selected\n", progname);
  41.       exit(ErrorExit);
  42.       }
  43.  
  44.    /* calc ideal scaling */
  45.    newpixels = (nbytes + granularity - 1) / granularity;
  46.    newrows = (newpixels + width - 1) / width;
  47.    newscale = memheight / newrows;
  48.  
  49.    /* set scaling, but no more than device or command option maximum */
  50.    if (memrow > newscale)
  51.       memrow = newscale;
  52.  
  53.    if (memrow <= 0)            /* sanity check, and extreme case */
  54.       memrow = 1;
  55.    if (newrows > memheight)
  56.       newrows = memheight;
  57.  
  58.    /* set total number of pixels and minimum y value */
  59.    mempixels = (word)newrows * (word)width;
  60.    if (mempixels > newpixels)
  61.       mempixels = newpixels;
  62.    ymin = memheight - newrows * memrow;
  63.    if (ymin < 0)
  64.       ymin = 0;
  65.  
  66.    /* if layout has changed (including first time), redraw everything */
  67.    if (memrow != oldscale)  {
  68.       devflood(C_Background);        /* clear screen */
  69.       if (oldscale == 0)
  70.          setmap(Marked, C_Marked);    /* init color map if first time */
  71.       legend();                /* replace legend */
  72.       if (title)            /* display centered title, if any */
  73.          mtext(title, 0, 0 + StatusLength + (TitleLength - strlen(title)) / 2,
  74.             C_Title, C_Background);
  75.       }
  76.    oldscale = memrow;
  77.  
  78.    /* display region sizes and garbage collection counts */
  79.    sprintf(sbuf, "%ld + %ld + %ld  (%ld+%ld+%ld+%ld)",
  80.       (long)stc.length, (long)str.length, (long)blk.length,
  81.       (long)nstatic, (long)nstring, (long)nblock, (long)nexplicit);
  82.    mtext(sbuf, 0, TextLength - strlen(sbuf) - 1, C_Rsizes, C_Background);
  83.    }
  84.  
  85. /*
  86.  * setdisp(region, key, addr) - set display parameters for a region.
  87.  */
  88. static novalue setdisp(r, key, addr)
  89. struct region *r;
  90. int key;
  91. word addr;
  92.    {
  93.    r->saddr = addr;            /* set screen address */
  94.    if (index(whichregs, key))
  95.       r->displ = r->length;        /* if displayed, set length */
  96.    else
  97.       r->displ = 0;            /* else set to zero */
  98.    }
  99.  
  100. /*
  101.  * paintblk(region, addr, len, color) - show a block.
  102.  */
  103. novalue paintblk(r, addr, len, color)
  104. struct region *r;
  105. word addr, len;
  106. int color;
  107.    {
  108.    if (r->displ == 0)
  109.       return;
  110.    mpaint((r->saddr + addr) / granularity, len / granularity, color, C_Bsep);
  111.    }
  112.  
  113. /*
  114.  * paintstr(addr, len, color1, color2) - show n string bytes.
  115.  *  Each pixel on the display represents multiple bytes.  A pixel in which any
  116.  *  string ends will be color2;  pixels corresponding to nonterminal characters
  117.  *  of one string will be color1.  This method makes long strings individually
  118.  *  distinguishable from each other and from runs of short strings, and allows
  119.  *  continuous output with no backtracking as the string space is allocated.
  120.  */
  121. novalue paintstr(addr, len, color1, color2)
  122. word addr, len;
  123. int color1, color2;
  124.    {
  125.    word s, e;
  126.  
  127.    if (str.displ == 0)            /* if not displaying strings, return */
  128.       return;
  129.    /* the start pixel is the first pixel wholly owned by this string */
  130.    /* the end pixel is the last pixel even partially reached */
  131.    s = (str.saddr + addr + granularity -1) / granularity;    /* start */
  132.    e = (str.saddr + addr + len - 1) / granularity;        /* end */
  133.    if (e >= s)                /* if any new pixels*/
  134.       mpaint(s, e - s + 1, color1, color2);
  135.    }
  136.  
  137. /*
  138.  * mpaint(addr, len, colr1, colr2) - paint a block or a string.
  139.  *
  140.  *  Just checks limits, then calls the device-dependent paint routine.
  141.  */
  142. hidden novalue mpaint(addr, len, color1, color2)
  143. word addr, len;
  144. int color1, color2;
  145.    {
  146.    if (addr < 0 || addr >= mempixels || len <= 0)
  147.       return;                /* if out of range, return */
  148.    if (addr + len > mempixels)
  149.       len = mempixels - addr;        /* set length to stay within bounds */
  150.    devpaint(addr, len, color1, color2); /* paint the device */
  151.    }
  152.  
  153. /*
  154.  * gcwait(key, msg) - pause with garbage collection message.
  155.  *
  156.  * Skips the pause if a -p option didn't include the given key character.
  157.  * After one EOF or 'G', never pauses again.  Returns whatever pause returns.
  158.  */
  159. int gcwait(key, msg)
  160. int key;
  161. char *msg;
  162.    {
  163.    if (reply != EOF && reply != 'g' && reply != 'G')
  164.       reply = mpause(key, msg);
  165.    return reply;
  166.    }
  167.  
  168. /*
  169.  * mpause(key, msg) - pause for response (or, in batchmode, print display).
  170.  *
  171.  * Skips the pause if a -p option didn't include the given key character.
  172.  * Returns the last character before '\n', 0 if none, or EOF.
  173.  */
  174. int mpause(key, msg)
  175. int key;
  176. char *msg;
  177.    {
  178.    static int c1, c2;
  179.  
  180.  
  181.    if (!index(whenpause, key))        /* if no pause/print of this display */
  182.       return 0;
  183.  
  184.    npauses++;                /* count this visit */
  185.  
  186.   if (batchmode) {            /* if batch, output snapshot */
  187.       mstatus(msg, C_Prompt);        /* set prompt message */
  188.       devsnap();            /* write frame */
  189.       mstatus("", C_Background);    /* clear the prompt */
  190.       if (npauses == pauselimit)    /* if limit hit, quit now */
  191.          mquit(NormalExit);
  192.       return 0;
  193.       }
  194.  
  195.    if (ttyi == NULL || c1 == EOF)  {    /* if no tty, or EOF already seen */
  196.       c1 = EOF;                /* fake EOF for return */
  197.       return EOF;
  198.       }
  199.    else {
  200.       fprintf(ttyo, "memmon pause: %s: ", msg);        /* issue prompt */
  201.       fflush(ttyo);
  202.       mstatus(msg, C_Prompt);                /* also post on screen*/
  203.       devflush();
  204.       c1 = 0;
  205.       while ((c2 = getc(ttyi)) != '\n' && c2 != '\r')    /* get char up to CR */
  206.          if (c2 == EOF || c2 == '\04')  {        /* handle eof */
  207.             c1 = EOF;
  208.             break;
  209.             }
  210.          else
  211.             c1 = c2;                    /* save previous char */
  212.       }
  213.  
  214.    if (c1 == 'q' || npauses == pauselimit)
  215.       mquit(NormalExit);        /* quit on 'q' or if limit hit */
  216.  
  217.    mstatus("", C_Background);        /* clear the prompt */
  218.    return c1;                /* return char before CR */
  219.    }
  220.  
  221. /*
  222.  * mstatus(msg, color) - post status message, erasing previous one.
  223.  */
  224. novalue mstatus(msg, color)
  225. char *msg;
  226. int color;
  227.    {
  228.    char buf[StatusLength];
  229.    int i;
  230.    char *p;
  231.  
  232.    for (i = 0, p = buf; i < StatusLength-1; i++)
  233.       if (*msg)
  234.          *p++ = *msg++;
  235.       else
  236.          *p++ = ' ';
  237.    *p++ = '\0';
  238.    mtext(buf, 0, 0, color, C_Background);
  239.    }
  240.  
  241. /*
  242.  * mtext(string, row, col, fgcolr, bgcolr) - display text in colored box.
  243.  *
  244.  * Just checks that we haven't suppressed the text, then calls devtext().
  245.  */
  246. novalue mtext(s, row, col, fg, bg)
  247. char *s;
  248. int row, col, fg, bg;
  249.    {
  250.    if (textrow > 0)
  251.       devtext(s, row, col, fg, bg);
  252.    }
  253.  
  254.